//#include <os/extend/avtime.h>
#include "includes.h"
#include "ioapi.h"
#include <sys/time.h>
#include <drivers/osd/osd_output.h>
#include "scrsaver.h"
#include "aw_windows.h"
#include "menu.h"
#include "../user.h"
static scr_direction = 0;
static PHWND bk_win = 0;
static unsigned char scr_saver_start = 0 ;

static unsigned char transparentlevel = 0xf;
AVTimer_t scrtimer = 0;
static unsigned short used_palette[] = 
{
//0xc4df,  0xf7,   0xc51f,  0xf7,   0xc55f,  0xf7,   0x5a0,  0xf8,   
//0x5a0,  0xf8,   0xc5e0,  0xf7,   0xc620,  0xf7,   0x660,  0xf8,   0x69f,  0xf8,   0x6df,  0xf8,   0x71f,  0xf8,   0x720,  0xf8,   
0x760,  0xf8,   0x7a0,  0xf8,   0xc7e0,  0xf7,   0x820,  0xf8,   0x860,  0xf8,   0xc89f,  0xf7,   0xc89f,  0xf7,   0x8e0,  0xf8,   
0x920,  0xf8,   0x960,  0xf8,   0xc99f,  0xf7,   0x9e0,  0xf8,   0xa20,  0xf8,   0xa20,  0xf8,   0xa5f,  0xf8,   0xaa0,  0xf8,   
0xcae0,  0xf7,   0xcb20,  0xf7,   0xcb5f,  0xf7,   0xba0,  0xf8,   0xba0,  0xf8,   0xbe0,  0xf8,   0xcc20,  0xf7,   0xc60,  0xf8,   
0xca0,  0xf8,   0xcdf,  0xf8,   0xd1f,  0xf8,   0xd20,  0xf8,   0xd60,  0xf8,   0xda0,  0xf8,   0xcde0,  0xf7,   0xe20,  0xf8,   
0xe60,  0xf8,   0xce9f,  0xf7,   0xce9f,  0xf7,   0xee0,  0xf8,   0xf20,  0xf8,   0xf60,  0xf8,   0xfa0,  0xf8,   0xfe0,  0xf8,   
0x1020,  0xf8,   0x101f,  0xf8,   0x105f,  0xf8,   0x10a0,  0xf8,   0xd0e0,  0xf7,   0xd120,  0xf7,   0xd160,  0xf7,   0x11a0,  0xf8,   
0x11a0,  0xf8,   0x11e0,  0xf8,   0xd21f,  0xf7,   0x1260,  0xf8,   0x12a0,  0xf8,   0x12e0,  0xf8,   0x1320,  0xf8,   0x1320,  0xf8,   
0x1360,  0xf8,   0x13a0,  0xf8,   0xd3e0,  0xf7,   0x1420,  0xf8,   0x1460,  0xf8,   0xd4a0,  0xf7,   0xd4df,  0xf7,   0xd4df,  0xf7,   
0x1520,  0xf8,   0x1560,  0xf8,   0x15a0,  0xf8,   0x15e0,  0xf8,   0x1620,  0xf8,   0x165f,  0xf8,   0x165f,  0xf8,   0x169f,  0xf8,   
0xd6e0,  0xf7,   0xd720,  0xf7,   0xd760,  0xf7,   0x17a0,  0xf8,   0x17e0,  0xf8,   0x17df,  0xf8,   0xd81f,  0xf7,   0xd85f,  0xf7,   
0x18a0,  0xf8,   0x18e0,  0xf8,   0x1920,  0xf8,   0x1960,  0xf8,   0x1960,  0xf8,   0x19a0,  0xf8,   0xd9df,  0xf7,   0xda1f,  0xf7,   
0x1a60,  0xf8,   0xdaa0,  0xf7,   0xdae0,  0xf7,   0x1ae0,  0xf8,   0x1b20,  0xf8,   0x1b60,  0xf8,   0x1ba0,  0xf8,   0xdbe0,  0xf7,   
0x1c20,  0xf8,   0x1c60,  0xf8,   0x1c5f,  0xf8,   0x1ca0,  0xf8,   0xdce0,  0xf7,   0x1d20,  0xf8,   0xdd60,  0xf7,   0xdda0,  0xf7,   
0x1de0,  0xf8,   0x1de0,  0xf8,   0xde20,  0xf7,   0x1e60,  0xf8,   0x1ea0,  0xf8,   0x1ee0,  0xf8,   0x1f20,  0xf8,   0x1f60,  0xf8,   
0x1f60,  0xf8,   0x1fa0,  0xf8,   0xdfdf,  0xf7,   0x2020,  0xf8,   0x2060,  0xf8,   0xe0a0,  0xf7,   0x20e0,  0xf8,   0xe0e0,  0xf7,   
0x2120,  0xf8,   0x2160,  0xf8,   0x21a0,  0xf8,   0x21e0,  0xf8,   0x2220,  0xf8,   0x2260,  0xf8,   0x2260,  0xf8,   0x22a0,  0xf8,   
0x22e0,  0xf8,   0x2320,  0xf8,   0xe360,  0xf7,   0x23a0,  0xf8,   0xe3df,  0xf7,   0x23e0,  0xf8,   0x2420,  0xf8,   0x245f,  0xf8,   
0x24a0,  0xf8,   0x24e0,  0xf8,   0x2520,  0xf8,   0x2560,  0xf8,   0xe5a0,  0xf7,   0x25a0,  0xf8,   0xe5e0,  0xf7,   0xe61f,  0xf7,   
0x2660,  0xf8,   0x26a0,  0xf8,   0x26e0,  0xf8,   0x2720,  0xf8,   0xe720,  0xf7,   0x2760,  0xf8,   0x279f,  0xf8,   0x27df,  0xf8,   
0x2820,  0xf8,   0x2860,  0xf8,   0x28a0,  0xf8,   0x28a0,  0xf8,   0xe8e0,  0xf7,   0x2920,  0xf8,   0xe960,  0xf7,   0xe99f,  0xf7,   
0x29e0,  0xf8,   0x2a20,  0xf8,   0xea20,  0xf7,   0x2a60,  0xf8,   0xeaa0,  0xf7,   0x2ae0,  0xf8,   0x2b20,  0xf8,   0x2b60,  0xf8,   
0x2ba0,  0xf8,   0x2ba0,  0xf8,   0x2be0,  0xf8,   0x2c20,  0xf8,   0xec5f,  0xf7,   0x2ca0,  0xf8,   0x2ce0,  0xf8,   0xed20,  0xf7,   
0x2d20,  0xf8,   0x2d60,  0xf8,   0x2da0,  0xf8,   0x2de0,  0xf8,   0x2e1f,  0xf8,   0x2e60,  0xf8,   0x2ea0,  0xf8,   0xeea0,  0xf7,   
0x2ee0,  0xf8,   0x2f20,  0xf8,   0xef60,  0xf7,   0x2fa0,  0xf8,   0xefdf,  0xf7,   0x3020,  0xf8,   0x3020,  0xf8,   0x3060,  0xf8,   
0x30a0,  0xf8,   0x30e0,  0xf8,   0x3120,  0xf8,   0x3160,  0xf8,   0xf19f,  0xf7,   0xf1a0,  0xf7,   0x31e0,  0xf8,   0xf220,  0xf7,   
0x3260,  0xf8,   0x32a0,  0xf8,   0x32e0,  0xf8,   0x3320,  0xf8,   0x331f,  0xf8,   0x3360,  0xf8,   0x33a0,  0xf8,   0x33e0,  0xf8,   
0x3420,  0xf8,   0x3460,  0xf8,   0x34a0,  0xf8,   0x34a0,  0xf8,   0xf4df,  0xf7,   0x3520,  0xf8,   0xf560,  0xf7,   0x35a0,  0xf8,   
0x35e0,  0xf8,   0x3620,  0xf8,   0x3660,  0xf8,   0x3660,  0xf8,   0xf69f,  0xf7,   0x36e0,  0xf8,   0x3720,  0xf8,   0x375f,  0xf8,   
0x37a0,  0xf8,   0x37e0,  0xf8,   0x37e0,  0xf8,   0x3820,  0xf8,   0xf860,  0xf7,   0x38a0,  0xf8,   0x38e0,  0xf8,   0xf920,  0xf7,   
0x3960,  0xf8,   0x3960,  0xf8,   0x39a0,  0xf8,   0x39e0,  0xf8,   0x3a20,  0xf8,   0x3a60,  0xf8,   0x3a9f,  0xf8,   0x3adf,  0xf8
} ;


unsigned char osdsettpval_scr(unsigned char level)//from 0 -- 15
{
    SetOSD240Color(used_palette,level);   
}

static void resetscrposition(void)
{
    INT32S iRet ;
	gOSDArg.drawing_coordinate.left = rand()%720 ;
	gOSDArg.drawing_coordinate.top = rand()%380 ;
	gOSDArg.drawing_coordinate.width = 226;
	gOSDArg.drawing_coordinate.height = 158;
	iRet =ioctl(gOSDHandle, 0x1d, &gOSDArg) ;
	//WARNNING_CATCH((iRet != AVFS_SUCCESSFUL), "OSD_MOVE_LEFT:Some thing wrong in OSD device...") ;
}

static void gen_customer_palette(unsigned char uCb, unsigned char uCr)
{
    int i;
    for(i = 0; i< 256 - 4; i++)
    {
        used_palette[2*i] = ((uCb&0x3)<<14)|((i+4)<<6)|uCr;
        used_palette[2*i+1] = ((uCb&0x3c)>>2)|(0x0<<4);
    }
}

static void scr_timer_func(void *arg)
{
#ifdef USE_NEW_SCREEN_SAVER 
    if(!scr_saver_start) {
        return ;
    }
#endif

    if((transparentlevel == 0)&&(!scr_direction))
    {
        scr_direction = 1;
        resetscrposition();
//        gen_customer_palette(rand()%64,rand()%64);
    }
    else if((transparentlevel == 0xf)&&scr_direction)
    {
        scr_direction = 0;
    }
    else if(scr_direction)
        transparentlevel++;
    else
        transparentlevel--;
    osdsettpval_scr(transparentlevel);
    
#ifdef USE_NEW_SCREEN_SAVER 
    scrtimer = AVTimerCreateFlag(OS_TICKS_PER_SEC>>2, scr_timer_func, NULL, AVTIMER_FLAG_NONE | AVTIMER_FLAG_AUTO_DELETE);
#endif

}

#ifndef USE_NEW_SCREEN_SAVER 
extern AVTimer_t keytimer;
#endif
PHWND screensaverstop(unsigned char type)
{
    int fd_video;
    
#ifdef USE_NEW_SCREEN_SAVER 
    if(!scr_saver_start) {
        return NULL;
    }
    
    scr_saver_start = 0 ;
#else
    if(!AVTimerIsEnabled(scrtimer)) {
        return NULL;
    }
    if (!scrtimer) {
        return NULL;
    }
    else {
        AVTimerDisable(scrtimer);
    }
#endif

    fd_video = open("/dev/video", O_RDONLY);
    if (fd_video < 0) {
        return NULL;
    }
    ioctl(fd_video, VIDEOIO_ENABLE);
    close(fd_video);    
    
#ifndef USE_NEW_SCREEN_SAVER 
    if((keytimer)&&(!AVTimerIsEnabled(keytimer))) {
        AVTimerEnable(keytimer);
    }
#endif

    if(bk_win == gpRootWin) {
        AWHideWindowByName("ScrSaver") ;
    }
    else {
        PHWND top = AWGetTopWindow(bk_win);
        if(type) {
            return top ;
        }
        AWShowWindow(top, 1);
    }
    return NULL;
}

unsigned char screensaverstatus(void)
{
    return scr_saver_start ;
}

unsigned char screensaverstart(void)
{
    int fd_video;

#ifdef USE_NEW_SCREEN_SAVER 
    scr_saver_start = 1 ;
#else
    if(keytimer) {
        AVTimerDisable(keytimer);
    }
    if(AVTimerIsEnabled(scrtimer)) {
        return 0;
    }
#endif

    fd_video = open("/dev/video", O_RDONLY);
    if (fd_video < 0) {
        return 0;
    }
    osdsettpval_scr(0xf);
    ioctl(fd_video, VIDEOIO_SMOKE);// make black backround
    close(fd_video);    
    
    bk_win = AWGetFocusWin() ;
    GUIShowWindow("ScrSaver") ;
    
#ifdef USE_NEW_SCREEN_SAVER 
    scrtimer = AVTimerCreateFlag(OS_TICKS_PER_SEC>>2, scr_timer_func, NULL, AVTIMER_FLAG_NONE | AVTIMER_FLAG_AUTO_DELETE);
#else
    if (!scrtimer) {
        scrtimer = AVTimerCreateFlag(OS_TICKS_PER_SEC>>2, scr_timer_func , NULL, AVTIMER_FLAG_PERIODIC); 
    }
    else {
        if(!AVTimerIsEnabled(scrtimer)) {
            AVTimerEnable(scrtimer);
        }
    }
#endif

    return 1;        
}
